//
//  ZJQRCoder.m
//  ZXingUseDemo
//
//  Created by mac on 14-12-17.
//  Copyright (c) 2014年 zhang jian. All rights reserved.
//

#import "ZJQRCoder.h"

@interface ZJQRCoder ()<ZXCaptureDelegate>
{
    ZXCapture *_capture;
    
    void (^_action)(UIImage *image, NSString *string);
}
@end


@implementation ZJQRCoder

+(id)sharedInstance
{
    static ZJQRCoder *coder = nil;
    if(coder == nil){
        coder = [[[self class] alloc] init];
    }
    return coder;
}


//传入文本和图片大小
//注意: 此处生成的默认是 QRCode 二维码
-(UIImage *)encodeWithString:(NSString *)string size:(CGSize)size
{
    NSError *error = nil;
    ZXMultiFormatWriter *writer = [ZXMultiFormatWriter writer];
    ZXBitMatrix* result = [writer encode:string
                                  format:kBarcodeFormatQRCode
                                   width:size.width
                                  height:size.height
                                   error:&error];
    if (result) {
        CGImageRef image = [[ZXImage imageWithMatrix:result] cgimage];
        
        // This CGImageRef image can be placed in a UIImage, NSImage, or written to a file.
        return [UIImage imageWithCGImage:image];
        
    } else {
        //NSString *errorMessage = [error localizedDescription];
        //NSLog(@"error = %@",errorMessage);
        
        return nil;
    }

}
//解码 -- 使用图片
//
-(NSString *)decodeWithImage:(UIImage *)image
{
    CGImageRef imageToDecode = image.CGImage;
    //CGImself.img.image;  // Given a CGImage in which we are looking for barcodes
    
    ZXLuminanceSource* source = [[ZXCGImageLuminanceSource alloc] initWithCGImage:imageToDecode] ;
    ZXBinaryBitmap* bitmap = [ZXBinaryBitmap binaryBitmapWithBinarizer:[ZXHybridBinarizer binarizerWithSource:source]];
    
    NSError* error = nil;
    
    // There are a number of hints we can give to the reader, including
    // possible formats, allowed lengths, and the string encoding.
    ZXDecodeHints* hints = [ZXDecodeHints hints];
    hints.encoding = NSUTF8StringEncoding;// StringEncoding;
    
    ZXMultiFormatReader* reader = [ZXMultiFormatReader reader];
    /* ZXResult* result = [reader decode:bitmap
     hints:hints
     error:&error];
     */
    ZXResult* result = [reader decode:bitmap
                                hints:nil
                                error:&error];
    if (result) {
        // The coded result as a string. The raw data can be accessed with
        // result.rawBytes and result.length.
        NSString* contents = result.text;
        
        // The barcode format, such as a QR code or UPC-A
        //ZXBarcodeFormat format = result.barcodeFormat;
        
        
        return contents;
        
    } else {

        //NSLog(@"没扫到");
        return nil;
    }

}
//解码 -- 通过摄像头拍摄
-(void)startCaptureInView:(UIView *)view
                     area:(CGRect)area
                   action:(void (^)(UIImage *image, NSString *string))action;
{
    _capture = [[ZXCapture alloc]init];
    _capture.camera = 1;
    _capture.rotation = 90;
    _capture.delegate = self;
    _capture.luminance = YES;
    //_capture.focusMode = AVCaptureFocusModeContinuousAutoFocus;
    
    CALayer *layer = _capture.luminance;//显示彩色照片
    layer.frame = area;
    layer.borderColor = [UIColor redColor].CGColor;
    layer.borderWidth = 3.0f;
    
    //  [self.capture setBinary:YES];  //显示黑白照片
    
    [view.layer addSublayer:_capture.luminance];
    
    //NSLog(@"%d,%d,%d",[capture hasBack],[capture hasFront],[capture hasTorch]);
    
    _capture.delegate = self;
    
    //保存block
    _action = action;
}

# pragma mark ZXcapture 代理方法
- (void)captureResult:(ZXCapture *)capture result:(ZXResult *)result{
    //NSLog(@"%s",__func__);
    if (result) {
        NSString* contents = result.text;
        
        // The barcode format, such as a QR code or UPC-A
        //ZXBarcodeFormat format = result.barcodeFormat;
        
        [capture stop];
        //NSLog(@"%@",contents);
        //[self.captureBt setTitle:@"扫一扫" forState:UIControlStateNormal];
        
        _action([UIImage imageWithCGImage:capture.lastScannedImage],contents);
        
    } else {
        //NSLog(@"中文出错");
    }
}
-(void)stopCapture
{
    [_capture stop];
}
@end
